<?php  
class ControllerModulecategory extends Controller {
	protected $category_id = 0;
	protected $path = array();
	
	protected function index() {
		$this->language->load('module/category');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/category');
		$this->load->model('catalog/product');
		$this->load->model('tool/seo_url');
		
		$path = '';
		if (isset($this->request->get['path'])) {
			$category_info = $this->model_catalog_category->getCategory($this->request->get['path']);
			if($category_info) {
				$path = $this->request->get['path'];
				while ($category_info['parent_id'] != 0) {
					$path = $category_info['parent_id'] . '_' . $path;
					$category_info = $this->model_catalog_category->getCategory($category_info['parent_id']);
				}
			} else {
				$path = 0;
			}
			$this->category_id = $this->request->get['path'];
		}
		
		if (isset($this->request->get['product_id'])) {
			$product_id = $this->request->get['product_id'];
		} else {
			$product_id = 0;
		}
		
		$product_info = $this->model_catalog_product->getproduct($product_id);
   		
		if ($product_info) {
			if ($product_info['category_id']) {
				$category_info = $this->model_catalog_category->getcategory($product_info['category_id']);
				if($category_info) {
				$path = $product_info['category_id'];
				while ($category_info['parent_id'] != 0) {
					$path = $category_info['parent_id'] . '_' . $path;
					$category_info = $this->model_catalog_category->getcategory($category_info['parent_id']);
				}
				}
				$this->category_id = $product_info['category_id'];
			} else {
				$path = 0;
			}
		}
		
		$this->path = explode('_', $path);
		
		$this->data['category'] = $this->getCategories(0);
												
		$this->id = 'category';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
		} else {
			$this->template = 'default/template/module/category.tpl';
		}
		
		$this->render();
  	}
	
	protected function getCategories($parent_id, $current_path = '') {
		$category_id = array_shift($this->path);
		
		$output = '';
		
		$results = $this->model_catalog_category->getCategories($parent_id);
		
		if ($results) {
			if(!$current_path){
				$output .= '<ul>';
			} else {
				$output .= '<ul class="child">';
			}
    	}
		
		foreach ($results as $result) {
			$new_path = $result['category_id'];
			
			$children = '';

			if ($this->category_id == $result['category_id']) {
				$select = ' class="select"';
			} else {
				$select = '';
			}
			
			$output .= '<li' . $select . '>';
			
				if ($category_id == $result['category_id']) {
					$children = $this->getCategories($result['category_id'], $new_path);
				}
				$output .= '<a ' . $select . 'href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $new_path))  . '">' . $result['name'] . '</a>';

        	$output .= $children;
        
        	$output .= '</li>'; 
		}
 
		if ($results) {
			$output .= '</ul>';
		}
		
		return $output;
	}		
}
?>